# coding:utf-8

import requests
from bs4 import BeautifulSoup


class YuanTong:

    def __init__(self, start_pro, start_city, des_pro, des_city):
        self.data = {
          "iniweights": 1,
          "weight": 1,
          "selPro": start_pro,
          "selCity": start_city,
          "selDis": None,
          "desPro": des_pro,
          "desCity": des_city,
          "desDis": None
        }
        # self.data = {
        #   "iniweights": 3,
        #   "weight": 3,
        #   "selPro": u"广东",
        #   "selCity": u"深圳",
        #   "selDis": None,
        #   "desPro": u"浙江",
        #   "desCity": u"金华",
        #   "desDis": None
        # }

        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
            "Origin": "http://www.yto.net.cn",
            "Referer": "http://www.yto.net.cn/gw/service/standardPrice.html"
        }

    def get_price(self):
        self.data["iniweights"] = self.data["weight"] = 1
        r = requests.post('http://www.yto.net.cn/gw/service/ProcessArea.aspx', data=self.data, headers=self.headers)
        content = BeautifulSoup(r.text, 'lxml')
        first_price = int(content.find_all('span')[0].get_text())
        self.data["iniweights"] = self.data["weight"] = 2
        r = requests.post('http://www.yto.net.cn/gw/service/ProcessArea.aspx', data=self.data, headers=self.headers)
        content = BeautifulSoup(r.text, 'lxml')
        second_price = int(content.find_all('span')[0].get_text())
        return [first_price, second_price - first_price]
